package v1

import (
	"github.com/gin-gonic/gin"
	"tdy-backend/app/core"
	memberService "tdy-backend/app/member/service"
	"tdy-backend/app/panoramic/dto"
	"tdy-backend/app/panoramic/service"
	"tdy-backend/global"
)

type SceneController struct {
}

// @Summary 加入场景至作品
// @Tags 管理端-场景管理
// @Param       token	header string true "登录标识"
// @Param	body	body	dto.SceneJoinDto true "添加场景至作品参数"
// @Router /Scene/JoinTo [post]
func (o *SceneController) JoinTo(c *gin.Context) {
	var err error
	var param dto.SceneJoinDto

	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, "参数缺失")
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	sceneService := service.SceneService{}
	sceneService.MakeORM(db).MakeMemberInfo(memberInfo)
	err = sceneService.JoinTo(&param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, nil, "场景添加成功")
}

// @Summary 删除作品中的场景
// @Tags 管理端-场景管理
// @Param       token	header string true "登录标识"
// @Param	body	body	dto.SceneDeleteDto true "删除场景参数"
// @Router /Scene/Delete [post]
func (o *SceneController) Delete(c *gin.Context) {
	var err error
	var param dto.SceneDeleteDto

	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, "参数缺失")
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	sceneService := service.SceneService{}
	sceneService.MakeORM(db).MakeMemberInfo(memberInfo)
	err = sceneService.Delete(&param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, nil, "删除成功")
}

func (o *SceneController) Edit(c *gin.Context) {

}

// @Summary 删除作品中的场景
// @Tags 管理端-场景管理
// @Param       token	header string true "登录标识"
// @Param	WorksId	query	uint true "作品id"
// @Router /Scene/ListAll [get]
func (o *SceneController) ListAll(c *gin.Context) {
	var err error
	var param dto.SceneListDto

	if err = c.Bind(&param); err != nil {
		core.CustomError(c, "参数缺失")
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	sceneService := service.SceneService{}
	sceneService.MakeORM(db).MakeMemberInfo(memberInfo)
	scenes := sceneService.ListAll(&param)
	core.Ok(c, scenes, "请求成功")
}
